package com.financing.platform.web.api;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ActionDTO;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.service.IActionService;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;
import com.financing.platform.vo.QueryMongoVo;

@Controller("ActionActionApi")
@RequestMapping("/api/action")
@Scope("prototype")
public class ActionAction extends BaseAction{
	
	@Autowired
	IActionService actionServiceImpl;
	
	@Autowired
	IFaUserService faUserServiceImpl;
	
	@Value("#{settings['mongoDBLink']}")
	private String mongoDBLink;
	
	@RequestMapping("/addAction.shtml")
	@ResponseBody
	public ResultMap addAction(HttpServletRequest request,
			HttpServletResponse response,ActionDTO actionDTO) {
		try{
			DTOdateSet.setCreateInfo(actionDTO, request.getSession());
			DTOdateSet.setUpdateInfo(actionDTO, request.getSession());
			actionServiceImpl.addAction(actionDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("addAction error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	@RequestMapping("/toDataList.shtml")
	@ResponseBody
	public String toDataList(HttpServletRequest request,
			HttpServletResponse response, QueryMongoVo data, Pager pager) {
		String string = "";
		try { 
			if(StringUtils.isBlank(data.getType())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				return JSONObject.fromObject(resultMap).toString();
			}
			if("".equals(data.getEndDate()) && "".equals(data.getStartDate())){
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
//				data.setEndDate(sdf.format(new Date()));
				data.setStartDate(sdf.format(new Date()));
			}
			//生产地址
			String sc=mongoDBLink+"statisitics/";
			//测试地址
//			String cs="http://192.168.1.88:8080/statistics/statisitics/";
			URL url=new URL(sc+data.getType()+"?"+data.toString() +"&pageSize="+pager.getPageSize() +"&pageNo=" + pager.getPageNo());
			URLConnection openConnection = url.openConnection();
			InputStream inputStream = openConnection.getInputStream();
			string=IOUtils.toString(inputStream);
			string=string.replaceAll("attributes", "attribute");
			if(data.getType().equals("queryDateList")){
				JSONObject json=JSONObject.fromObject(string);
				JSONObject attribute = json.getJSONObject("attribute");
				JSONArray list = attribute.getJSONArray("list");
				for (Object object : list) {
					try {
						JSONObject j=(JSONObject)object;
						String userId = j.getString("userId");
						String loginName = j.getString("loginName");
						if(StringUtils.isBlank(userId)&&StringUtils.isBlank(loginName))
							continue;
						FaUserDTO faUserDTO=new FaUserDTO();
						if(StringUtils.isNotBlank(userId))
							faUserDTO.setId(userId);
						if(StringUtils.isNotBlank(loginName))
							faUserDTO.setuLoginName(loginName);
						//查询FA信息
						try {
							faUserDTO = faUserServiceImpl.getFaUserView(faUserDTO);
							j.put("fwgw", faUserDTO.getLicaishiManagerUser());
							j.put("realName", faUserDTO.getuRealName());
							j.put("loginNum", faUserDTO.getLoginNum());
						} catch (Exception e) {
						log.error("not have error !",e);
						}
					} catch (Exception e) {
						log.error("JSONObject is null",e);
					}
				}
				string=json.toString();
			}
		} catch (IOException e) {
			log.error("toDataList error !",e);
		}
		return string;
	}
}